import Vue from 'vue';
import axios from 'axios';
import router from '../router';
import {
    Message
} from 'element-ui'


const service = axios.create({
    // timeout: 5000
});

service.interceptors.request.use(
    config => {
        const token = localStorage.getItem('Token');
        if (token) {
            config.headers['Authorization'] = 'Bearer' + token
        }
        return config;
    },
    error => {
        console.log(error);
     
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            //单点登录判断
            if (response.data.status_code === '1006') {
                sessionStorage.clear('Token')
                router.push({
                    path: "/login"
                });
                Message({
                    message: response.data.message,
                    type: 'error',
                })
                return
            }
            return response.data;
        } else {
            Promise.reject();
        }
    },
    error => {
        if (error.response.status == 500) {
       
        } else {
            Message({
                message: error.response.data.error,
                type: 'error',
            })
            return
        }
        return Promise.reject();
    }
);

export default service;